﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Core.DataAccess.Model.Project.Entrepot;
using Core.Framework.Model.Common;
using Core.Framework.Util;
using Core.IBusiness.IEntrepotModule;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Core.Web.Controllers.Entrepot
{

    /// <summary>
    /// 产品分类
    /// </summary>
    [Route(".v1/Entrepot/[controller]")]
    [ApiController]
    [ApiVerification(IBusiness.IProjectModule.Model.APIServiceEnum.Shop)]
    public class ProductTypeController : BaseApiController
    {

        public ProductTypeController(IEntrepot iEntrepot) :
            base(iEntrepot: iEntrepot)
        { }

        /// <summary>
        /// 获取分类集合
        /// </summary>
        /// <param name="pagination">分页实体</param>
        /// <returns></returns>
        [HttpPost(".page")]
        [IgnoreAuthorize]
        public IResult GetProductType(Pagination pagination)
        {

            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.GetProductType(
                        iProjectInfo.Token,
                        pagination),pagination);
                });
        }

        /// <summary>
        /// 获取指定分类
        /// </summary>
        /// <param name="type_key">分类关键字</param>
        /// <returns></returns>
        [HttpGet("{type_key}")]
        [IgnoreAuthorize]
        public IResult GetProductItemType(int type_key)
        {

            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.GetProductItemType(
                        type_key,
                        iProjectInfo.Token));
                },
                new Tuple<bool, string>(type_key > 0, type_key > 0 ? "" : "params: type_key is null"));
        }





        /// <summary>
        /// 添加分类
        /// </summary>
        /// <param name="model">字典实体</param>
        /// <returns></returns>
        [HttpPost]
        public IResult POST(ProductType model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.ProductTypeExec(
                        model,SqlExecType.insert));
                }, model.IsValid(IsValidEnum.reg));
        }



        /// <summary>
        /// 修改分类信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public IResult Put(ProductType model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.ProductTypeExec(
                        model,
                        SqlExecType.update));
                }, model.IsValid(IsValidEnum.update));
        }





        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="id">分类关键字</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IResult Delete(int id)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.ProductTypeExec(new ProductType
                    {
                        Id = id
                    }, SqlExecType.delete));
                },
                new Tuple<bool, string>(id > 0, id > 0 ? "" : "params: id is null"));
        }



    }
}